package com.ch.mobilesha.common;

import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.ch.mobilesha.base.MobileShaApplication;
import com.ch.mobilesha.tools.NetTools;


public class HttpClientUtils {
	
	private HttpClientUtils(){}
	
	private static DefaultHttpClient client;
	
	/**
	 * 获取HttpClient
	 * @return
	 */
	public static synchronized DefaultHttpClient getHttpClient() {
		context = MobileShaApplication.getSelf();
		if(client != null) 
			return client;
		else {
			if(context != null)
				client = createDefaultHttpClient();
			return client;
		}
	}
	private static Context context;

	private static HttpHost target;
    
    public static HttpHost getTarget() {
		return target;
	}
	
    /* -----------------------------新的HttpClient--------------------------------- */
	private final static int MAX_TOTAL_CONNECTIONS = 150; 
     
	private final static int WAIT_TIMEOUT = 1000; 
     
	private final static int MAX_ROUTE_CONNECTIONS = 80; 
     
	private final static int CONNECT_TIMEOUT = 12000; 
     
	private final static int READ_TIMEOUT = 20000;
	
	/**
	 * 创建DefaultHttpClient
	 * @return
	 */
	public static DefaultHttpClient createDefaultHttpClient() {
       	HttpParams params = createHttpParams();
        SchemeRegistry registry = new SchemeRegistry(); 
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
        registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
        
        ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params, registry); 
        DefaultHttpClient httpclient = new DefaultHttpClient(connectionManager, params);
        if(NetTools.getNetWorkType(context) != NetTools.TYPE_WIFI) {
        	Uri uri = Uri.parse("content://telephony/carriers/preferapn");   
        	Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);     
        	if(cursor != null && cursor.moveToNext()) {   
        		String proxyStr = cursor.getString(cursor.getColumnIndex("proxy"));    
        		if(proxyStr != null && !"".equals(proxyStr.trim())) {
        			target = new HttpHost("YOUR_TARGET_IP", 80, "http");
        			HttpHost proxy = new HttpHost(proxyStr, 80, "http");
        			httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        		}
        	}
        } else{
  			target = null;
  		}
       
		return httpclient;
    }
    
	/**
	 * 设置HttpParams
	 * @return
	 */
    private static final HttpParams createHttpParams() {
    	HttpParams httpParams = new BasicHttpParams(); 
    	HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
        // 设置最大连接数 
        ConnManagerParams.setMaxTotalConnections(httpParams, MAX_TOTAL_CONNECTIONS); 
        // 设置从连接池获取连接的最大等待时间 
        ConnManagerParams.setTimeout(httpParams, WAIT_TIMEOUT); 
        // 设置每个路由最大连接数 
        ConnPerRouteBean connPerRoute = new ConnPerRouteBean(MAX_ROUTE_CONNECTIONS); 
        ConnManagerParams.setMaxConnectionsPerRoute(httpParams, connPerRoute); 
    	// 设置连接超时时间 
        HttpConnectionParams.setConnectionTimeout(httpParams, CONNECT_TIMEOUT); 
        // 设置socket读取数据超时时间 
        HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT); 
        // 设置以Socket 缓存大小
        HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
        HttpConnectionParams.setStaleCheckingEnabled(httpParams, false);
        // 设置重定向，缺省为 true , 这里设置成不重定向
        HttpClientParams.setRedirecting(httpParams, false);

		return httpParams;
	}
}
